package com.watson.websocket.controller1;


import com.watson.websocket.service1.WsSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ws")
public class WebSocketController1 {

    @Autowired
    private WsSendService wsSendService;

    //服务端给客户端发送消息
    @RequestMapping(value="/sendMsg", method = RequestMethod.GET)
    public void sendMsg(){
        wsSendService.broadcastMsg("this is server' msg");
    }

    //接收客户端发送的消息
    @MessageMapping("/address1")
    @SendTo("/topic/broadcast/message")
    public String address1(String msg){
        System.out.println(msg);
        return "address1' msg:"+msg;
    }
}
